package pl.edu.agh.mwo.ui.action;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFileChooser;

import pl.edu.agh.mwo.model.MenadzerPracownikow;
import pl.edu.agh.mwo.ui.app.MainApp;

public class EksportAction extends AbstractAction {

	private static final long serialVersionUID = -2206397237479809080L;
	private MainApp mainApp;
	private MenadzerPracownikow mp;

	public EksportAction(MainApp component, MenadzerPracownikow mp) {
		this.mainApp = component;
		this.mp = mp;
		putValue(NAME, "Eksportuj dane");
		putValue(SHORT_DESCRIPTION, "Shows about dialog");
		putValue(Action.MNEMONIC_KEY, new Integer('A'));

	}

	@Override
	public void actionPerformed(ActionEvent event) {
		final JFileChooser fc = new JFileChooser();

		int returnVal = fc.showSaveDialog(mainApp.getFrame());

		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fc.getSelectedFile();

			PrintWriter zapis;
			try {
				zapis = new PrintWriter(file);

				mainApp.getTable().getModel().getRowCount();

				for (int i = 0; i < mainApp.getTable().getRowCount(); i++) {
					for (int j = 0; j < mainApp.getTable().getColumnCount(); j++) {
						String value = mainApp.getTable().getValueAt(i, j).toString();
						
						zapis.print(value);
						zapis.print(",");
					}
					zapis.println();
				}

				zapis.println();
				zapis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}

		} else {

		}

	}

}
